{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9f528a4c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using the Community license in this session. If you have a full Xpress license, first set the XPAUTH_PATH environment variable to the full path to your license file, xpauth.xpr, and then restart Python. If you want to use the FICO Community license and no longer want to see this message, set the XPAUTH_PATH environment variable to: /home/maged/anaconda3/envs/balance/lib/python3.8/site-packages/xpress/license/community-xpauth.xpr\n",
      "NB: setting XPAUTH_PATH will also affect any other Xpress products installed on your system.\n"
     ]
    }
   ],
   "source": [
    "# Importing modules\n",
    "try:\n",
    "    import hsbalance as hs;\n",
    "except ImportError:\n",
    "    !pip install hsbalance\n",
    "    import hsbalance as hs;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7f60a45",
   "metadata": {},
   "source": [
    "# Introduction\n",
    "In real turbo-machinery balancing, usually we need to solve the problem for various speeds and load conditions. This can be confusing.    \n",
    "`hsbalance` provides a convienent tool to explicity and easily arrange the work."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "04810b6f",
   "metadata": {},
   "source": [
    "# Example"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a5efab10",
   "metadata": {},
   "source": [
    "## Speed 1500"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d6aa95ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Writing A, alpha at speed 1500 in mathmatical form from site data\n",
    "A_1500 = [['20@40'],\n",
    "          ['12@59']]\n",
    "alpha_1500 = [['0.3@24', '0.5@55'],\n",
    "              ['0.6@55', '0.44@97']]\n",
    "# Converting the mathmatical data to cartesian.\n",
    "A = hs.convert_math_cart(A_1500)\n",
    "# Create alpha instance\n",
    "alpha = hs.Alpha(name='Speed = 1500')\n",
    "alpha.add(hs.convert_math_cart(alpha_1500))\n",
    "# Create condition for A and alpha at speed 1500\n",
    "condition_1500 =hs.Condition(name='Speed = 1500')\n",
    "condition_1500.add(A=A, alpha=alpha)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e8b4df9b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "++++++++++++++++++++++++++++++++++++++++\n",
      "Influence Coefficient Matrix\n",
      "++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "++++++++++++++++++++++++++++++++++++++++\n",
      "Name\n",
      "==============================\n",
      "Speed = 1500\n",
      "==============================\n",
      "End of Name\n",
      "++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "                   \n",
      "++++++++++++++++++++++++++++++++++++++++\n",
      "Coefficient Values\n",
      "==============================\n",
      "             Plane 1      Plane 2\n",
      "Sensor 1  0.3 @ 24.0   0.5 @ 55.0\n",
      "Sensor 2  0.6 @ 55.0  0.44 @ 97.0\n",
      "==============================\n",
      "End of Coefficient Values\n",
      "++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "                   \n"
     ]
    }
   ],
   "source": [
    "print(alpha)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dd8e6ac6",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "96cc3c6c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "Operation Condition\n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "Name\n",
      "========================================\n",
      "Speed = 1500\n",
      "========================================\n",
      "End of Name\n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "                   \n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "Condition IC Matrix\n",
      "========================================\n",
      "\n",
      "++++++++++++++++++++++++++++++++++++++++\n",
      "Influence Coefficient Matrix\n",
      "++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "++++++++++++++++++++++++++++++++++++++++\n",
      "Name\n",
      "==============================\n",
      "Speed = 1500\n",
      "==============================\n",
      "End of Name\n",
      "++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "                   \n",
      "++++++++++++++++++++++++++++++++++++++++\n",
      "Coefficient Values\n",
      "==============================\n",
      "             Plane 1      Plane 2\n",
      "Sensor 1  0.3 @ 24.0   0.5 @ 55.0\n",
      "Sensor 2  0.6 @ 55.0  0.44 @ 97.0\n",
      "==============================\n",
      "End of Coefficient Values\n",
      "++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "                   \n",
      "========================================\n",
      "End of Condition IC Matrix\n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "                   \n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "Initial Vibration\n",
      "========================================\n",
      "            Vibration\n",
      "Sensor 1  20.0 @ 40.0\n",
      "Sensor 2  12.0 @ 59.0\n",
      "========================================\n",
      "End of Initial Vibration\n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "                   \n"
     ]
    }
   ],
   "source": [
    "print(condition_1500)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4dbe4eea",
   "metadata": {},
   "source": [
    "## Speed 2300"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3bac5667",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Writing A, alpha at speed 2300 in mathmatical form from site data\n",
    "A_2300 = [['12@33'],\n",
    "          ['54@34']]\n",
    "alpha_2300 = [['0.112@24', '0.17@77'],\n",
    "              ['0.52@23', '0.67@45']]\n",
    "# Converting the mathmatical data to cartesian.\n",
    "A = hs.convert_math_cart(A_2300)\n",
    "# Create alpha instance\n",
    "alpha = hs.Alpha(name='Speed = 2300')\n",
    "alpha.add(hs.convert_math_cart(alpha_2300))\n",
    "# Create condition for A and alpha at speed 2300\n",
    "condition_2300 =hs.Condition(name='Speed = 2300')\n",
    "condition_2300.add(A=A, alpha=alpha)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd8692f0",
   "metadata": {},
   "source": [
    "For verbosity, you can print the condition, see what matrices are attached to it and confirm your data is correct."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "70bcc8a2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "Operation Condition\n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "Name\n",
      "========================================\n",
      "Speed = 2300\n",
      "========================================\n",
      "End of Name\n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "                   \n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "Condition IC Matrix\n",
      "========================================\n",
      "\n",
      "++++++++++++++++++++++++++++++++++++++++\n",
      "Influence Coefficient Matrix\n",
      "++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "++++++++++++++++++++++++++++++++++++++++\n",
      "Name\n",
      "==============================\n",
      "Speed = 2300\n",
      "==============================\n",
      "End of Name\n",
      "++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "                   \n",
      "++++++++++++++++++++++++++++++++++++++++\n",
      "Coefficient Values\n",
      "==============================\n",
      "               Plane 1      Plane 2\n",
      "Sensor 1  0.112 @ 24.0  0.17 @ 77.0\n",
      "Sensor 2   0.52 @ 23.0  0.67 @ 45.0\n",
      "==============================\n",
      "End of Coefficient Values\n",
      "++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "                   \n",
      "========================================\n",
      "End of Condition IC Matrix\n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "                   \n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "Initial Vibration\n",
      "========================================\n",
      "            Vibration\n",
      "Sensor 1  12.0 @ 33.0\n",
      "Sensor 2  54.0 @ 34.0\n",
      "========================================\n",
      "End of Initial Vibration\n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "                   \n"
     ]
    }
   ],
   "source": [
    "print(condition_2300)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cc09367f",
   "metadata": {},
   "source": [
    "## Modeling"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "dbf0774e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([['48.092 @ 81.2'],\n",
       "       ['82.483 @ 190.8']], dtype='<U14')"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model = hs.LeastSquares(conditions=[condition_1500, condition_2300])\n",
    "hs.convert_cart_math(model.solve())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "04c79bfa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9.4131"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.rmse()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "bef5cbfc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "MODEL\n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "MODEL TYPE\n",
      "==================================================\n",
      "LeastSquares\n",
      "==================================================\n",
      "End of MODEL TYPE\n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "                   \n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "CONDITIONS\n",
      "==================================================\n",
      "\n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "Operation Condition\n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "Name\n",
      "========================================\n",
      "Speed = 1500\n",
      "========================================\n",
      "End of Name\n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "                   \n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "Condition IC Matrix\n",
      "========================================\n",
      "\n",
      "++++++++++++++++++++++++++++++++++++++++\n",
      "Influence Coefficient Matrix\n",
      "++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "++++++++++++++++++++++++++++++++++++++++\n",
      "Name\n",
      "==============================\n",
      "Speed = 1500\n",
      "==============================\n",
      "End of Name\n",
      "++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "                   \n",
      "++++++++++++++++++++++++++++++++++++++++\n",
      "Coefficient Values\n",
      "==============================\n",
      "             Plane 1      Plane 2\n",
      "Sensor 1  0.3 @ 24.0   0.5 @ 55.0\n",
      "Sensor 2  0.6 @ 55.0  0.44 @ 97.0\n",
      "==============================\n",
      "End of Coefficient Values\n",
      "++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "                   \n",
      "========================================\n",
      "End of Condition IC Matrix\n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "                   \n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "Initial Vibration\n",
      "========================================\n",
      "            Vibration\n",
      "Sensor 1  20.0 @ 40.0\n",
      "Sensor 2  12.0 @ 59.0\n",
      "========================================\n",
      "End of Initial Vibration\n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "                   \n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "Operation Condition\n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "Name\n",
      "========================================\n",
      "Speed = 2300\n",
      "========================================\n",
      "End of Name\n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "                   \n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "Condition IC Matrix\n",
      "========================================\n",
      "\n",
      "++++++++++++++++++++++++++++++++++++++++\n",
      "Influence Coefficient Matrix\n",
      "++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "++++++++++++++++++++++++++++++++++++++++\n",
      "Name\n",
      "==============================\n",
      "Speed = 2300\n",
      "==============================\n",
      "End of Name\n",
      "++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "                   \n",
      "++++++++++++++++++++++++++++++++++++++++\n",
      "Coefficient Values\n",
      "==============================\n",
      "               Plane 1      Plane 2\n",
      "Sensor 1  0.112 @ 24.0  0.17 @ 77.0\n",
      "Sensor 2   0.52 @ 23.0  0.67 @ 45.0\n",
      "==============================\n",
      "End of Coefficient Values\n",
      "++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "                   \n",
      "========================================\n",
      "End of Condition IC Matrix\n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "                   \n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "Initial Vibration\n",
      "========================================\n",
      "            Vibration\n",
      "Sensor 1  12.0 @ 33.0\n",
      "Sensor 2  54.0 @ 34.0\n",
      "========================================\n",
      "End of Initial Vibration\n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "                   \n",
      "==================================================\n",
      "End of CONDITIONS\n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "                   \n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "SOLUTION\n",
      "==================================================\n",
      "        Correction Masses\n",
      "Plane 1     48.092 @ 81.2\n",
      "Plane 2    82.483 @ 190.8\n",
      "==================================================\n",
      "End of SOLUTION\n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "                   \n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "RMSE\n",
      "==================================================\n",
      "9.4131\n",
      "==================================================\n",
      "End of RMSE\n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "                   \n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "Resiudal Vibration Expected\n",
      "==================================================\n",
      "         Expected Vibration\n",
      "Sensor 1     12.091 @ 243.6\n",
      "Sensor 2      5.571 @ 230.4\n",
      "Sensor 3      8.426 @ 344.3\n",
      "Sensor 4      11.564 @ 49.1\n",
      "==================================================\n",
      "End of Resiudal Vibration Expected\n",
      "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n",
      "\n",
      "                   \n"
     ]
    }
   ],
   "source": [
    "print(model.info())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c9698fc6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:balance] *",
   "language": "python",
   "name": "conda-env-balance-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
